package com.augurit.common.system.web.org;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.web.org.form.OmUserGroupForm;
import com.augurit.common.system.web.org.form.OmUserOrgForm;
import com.augurit.common.system.web.org.form.OmUserPositionForm;
import com.augurit.common.util.ExtUtils;

/**
 * 用户组织归属Action类
 */
@SuppressWarnings("serial")
public class UserPartyAction extends ExtBaseAction {

	// Service 引用
	@Autowired
	private IOmUserService omUserService;

	
	// 基本属性
	private Page<OmUserOrgForm> orgPage = new Page<OmUserOrgForm>(15);
	private Page<OmUserGroupForm> groupPage = new Page<OmUserGroupForm>(15);
	private Page<OmUserPositionForm> posPage = new Page<OmUserPositionForm>(15);
	
	
	private Long userId;	//用户ID
	private Long orgId;		//所属机构ID
	private Long posId;		//所属岗位ID
	private Long[] uoIds;	//OmUserOrg对象ID数组
	private Long[] ugIds;	//OmUserGroup对象Id数组
	private Long[] upIds;	//OmUserPosition对象ID数组
	
	/**
	 * 获取指定用户所属机构列表
	 */
	public String getRelatedOrgs() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), orgPage);
		omUserService.getRelatedOrgs(orgPage, userId);
		this.extRenderGridJson(orgPage);
		return null;
	}
	
	/**
	 * 获取指定用户所属工作组列表
	 */
	public String getRelatedGroups() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), groupPage);
		omUserService.getRelatedGroups(groupPage, userId);
		this.extRenderGridJson(groupPage);
		return null;
	}
	
	/**
	 * 获取指定用户所属机构岗位列表
	 */
	public String getRelatedOrgPositions() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), posPage);
		omUserService.getRelatedPositions(posPage, userId, OrgConstant.PARTY_TYPE_ORG);
		this.extRenderGridJson(posPage);
		return null;
	}
	
	/**
	 * 获取指定用户所属工作组岗位列表
	 * @return
	 * @throws Exception
	 */
	public String getRelatedGroupPositions() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), posPage);
		omUserService.getRelatedPositions(posPage, userId, OrgConstant.PARTY_TYPE_GROUP);
		this.extRenderGridJson(posPage);
		return null;
	}

	/**
	 * 删除用户与机构的关联关系
	 */
	public String deleteOrgRelation() throws Exception {
		omUserService.deleteOmUserOrg(uoIds);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 删除用户与工作组的关联关系
	 */
	public String deleteGroupRelation() throws Exception {
		omUserService.deleteOmUserGroup(ugIds);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 删除用户与岗位的关联关系
	 */
	public String deletePosRelation() throws Exception {
		omUserService.deleteOmUserPosition(upIds);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 设置主机构
	 */
	public String setMainOrg() throws Exception {
		omUserService.setMainOrg(userId, orgId);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 设置主岗位
	 */
	public String setMainPos() throws Exception {
		omUserService.setMainPos(userId, posId);
		this.extRenderSuccess();
		return null;
	}
	
	
	// 属性getter和setter方法
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Long[] getUoIds() {
		return uoIds;
	}

	public void setUoIds(Long[] uoIds) {
		this.uoIds = uoIds;
	}

	public Long[] getUpIds() {
		return upIds;
	}

	public void setUpIds(Long[] upIds) {
		this.upIds = upIds;
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}

	public Long getPosId() {
		return posId;
	}

	public void setPosId(Long posId) {
		this.posId = posId;
	}

	public Long[] getUgIds() {
		return ugIds;
	}

	public void setUgIds(Long[] ugIds) {
		this.ugIds = ugIds;
	}
}
